package com.lt.base;

import android.app.ActivityManager;
import android.content.Context;

import com.hjq.toast.Toaster;
import com.lt.app.ActivityStackManager;
import com.lt.app.NotificationCenter;
import com.lt.app.ResHelper;

/**
 * 这里是程序主入口
 * app的Application必须继承BaseApplication,不然library包很多方法调用都会出错
 */
public abstract class BaseApplication extends android.app.Application {

    private Boolean blMainProcess;
    private NotificationCenter notificationCenter = null;
    private static BaseApplication instance;

    public abstract boolean isDebug();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        ActivityStackManager.getInstance().init(this);
        Toaster.init(this);
    }

    public static BaseApplication getInstance() {
        return instance;
    }

    public static Context getContext() {
        return getInstance().getApplicationContext();
    }

    public NotificationCenter getNotificationCenter() {
        if (notificationCenter == null)
            notificationCenter = new NotificationCenter();
        return notificationCenter;
    }

    /**
     * 判断当前进程是否是应用的主进程
     */
    public boolean isMainProcess() {
        if (blMainProcess == null) {
            blMainProcess = false;
            int pid = android.os.Process.myPid();
            ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            if (activityManager != null) {
                for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
                    if (appProcess.pid == pid) {
                        blMainProcess = getApplicationInfo().packageName.equals(appProcess.processName);
                    }
                }
            }
        }
        return blMainProcess;
    }

    public void showToast(Object o) {
        String msg;
        if (o instanceof Integer) {
            msg = ResHelper.getString((int) o);
        } else {
            msg = String.valueOf(o);
        }
        Toaster.show(msg);
    }
}
